package com.ccb.testcenter.tools;

/**
 * 
 */


import java.util.List;
import net.minidev.json.JSONValue;
import org.apache.log4j.Logger;
import com.jayway.jsonpath.JsonPath;
/**
 * @author congyang
 *
 */
public class JsonParseTool {

	private static final Logger logger = Logger.getLogger(JsonParseTool.class);

	public static String getDataFromAbsolutePath(String jsonString, String key) {
		if (!JSONValue.isValidJson(jsonString)) {
			logger.info("json string is not valid");
			return "";
		}
		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}

		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$.").append(key);
			key = sb.toString().trim();
		}
		return JsonPath.read(jsonString, key).toString();

	}

	public static String getDataFromRelativePath(String jsonString, String key) {

		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$..").append(key);
			key = sb.toString().trim();
		}
		//fastjson 1.2.7不支持相对路径
		/*JSONObject jsonObject = JSON.parseObject(jsonString);
		return (String) JSONPath.eval(jsonObject, key);*/
		List<String> lists = JsonPath.read(jsonString, key);
		return lists.get(0).toString();

	}
	
	
	
	public static String getRequestDataFromAbsolutePath(String jsonString, String key) {

		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (key.equals("")) {
			logger.info("key string is null");
			return "";
		}

		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$.requestData.").append(key);
			key = sb.toString().trim();
		}
		return JsonPath.read(jsonString, key);

	}

	public static String getRequestDataFromRelativePath(String jsonString, String key) {

		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (key.equals("")) {
			logger.info("key string is null");
			return "";
		}

		List<String> lists = null;
		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$.requestData..").append(key);
			key = sb.toString().trim();

		}
		lists = JsonPath.read(jsonString, key);
		return lists.get(0).toString();

	}

	public static String getResponseDataFromAbsolutePath(String jsonString, String key) {

		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (key.equals("")) {
			logger.info("key string is null");
			return "";
		}
		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$.responseData.").append(key);
			key = sb.toString().trim();
		}
		return JsonPath.read(jsonString, key);

	}

	public static String getResponseDataFromRelativePath(String jsonString, String key) {

		if (jsonString.equals("")) {
			logger.info("json string is null");
			return "";
		}
		if (key.equals("")) {
			logger.info("key string is null");
			return "";
		}
		if (!key.startsWith("$")) {
			StringBuilder sb = new StringBuilder();
			sb.append("$.responseData..").append(key);
			key = sb.toString().trim();
		}
		List<String> lists = JsonPath.read(jsonString, key);
		return lists.get(0).toString();
	}

}
